
var PageOptionsGenerales = new (function() {

	this.passwordFreebox = null;
	this.selectTorrentExceptions = null;
	
	this.distantAccess = null;
	this.distantAccessIP = null;
	this.distantAccessPort = null;
	this.distantAccessFieldset = null;
	
	this.torrentAdvanced = null;
	this.torrentContextMenu = null;
	
	
	this.torrentExceptionsInput = null;
	this.torrentExceptionsAdd = null;
	
	
	this.btnSave = null;
	
	this.elements = ["passwordFreebox", "distantAccess", "distantAccessIP", "distantAccessPort",
						"torrentAdvanced", "torrentContextMenu", "torrentExceptionsAdd", "torrentExceptionsInput",
						"btnSave", "distantAccessFieldset"];
	
	var TEXTS = {
		"aide-info-gestion-torrent-avance": 	"En activant cette option, l'extension téléchargera les fichiers torrents pour envoyer le fichier .torrent téléchargé directement à la Freebox.\n"+
												"En plus de cela, l'extension pourra détecter automatiquement (à 99%) s'il s'agit d'un fichier .torrent où d'un fichier classique.\n\n"+
												"Néanmoins si un site pose problème, n'hésitez pas à utiliser les exceptions torrents situés plus en dessous.",
		"aide-info-distant-access-port":		"Port utilisé par la Freebox par défaut: 80"
	};
	
	
	var informationIcons = ["info-gestion-torrent-avance", "info-distant-access-port"];
	
	var $this = this;
	
	
	this.autocompleteIPPort = function(text) {
		var ipport = text;
		var ip = text;
		var port = null;
		
		if (text.indexOf("http://") == 0) {
			ipport = text.substring(7);
		}
		
		if (ipport.indexOf(":")) {
			var aipport = ipport.split(":");
			ip = aipport[0];
			port = aipport[1];
		}
		$this.distantAccessIP.value = ip;
		if (port) {
			$this.distantAccessPort.value = port;
		}
	};
	
	this.distantAccessCheckChange = function() {
		if ($this.distantAccess.checked) {
			$this.distantAccessFieldset.className = "";
			$this.distantAccessIP.disabled = false;
			$this.distantAccessPort.disabled = false;
		} else {
			$this.distantAccessFieldset.className = "disabled";
			$this.distantAccessIP.disabled = true;
			$this.distantAccessPort.disabled = true;
		}
	}
	
	this.addURLToTorrentExceptions = function() {
		var url = $this.torrentExceptionsInput.value;
		if (url != "" && url != "http://") {
			$this.selectTorrentExceptions.addOption(url, url);
		}
	};

	this.loadFromOptions = function() {
		var options = FreeboxLinkAPI.getOptions();
		
		if (options.passwordFreebox != null) {
			$this.passwordFreebox.value = options.passwordFreebox;
		}
		
		$this.distantAccess.checked = options.distantAccess;
		$this.distantAccessIP.value = options.distantAccessIP;
		$this.distantAccessPort.value = options.distantAccessPort;
		
		$this.torrentAdvanced.checked = options.torrentAdvanced;
		$this.torrentContextMenu.checked = options.torrentContextMenu;
		
		for (var i = 0; i < options.torrentExceptions.length; i++) {
			var url = options.torrentExceptions[i];
			$this.selectTorrentExceptions.addOption(url, url);			
		}
		
	};
	
	this.saveToOptions = function() {
		console.log("Saving...");
		var options = FreeboxLinkAPI.getOptions();
		
		options.passwordFreebox = $this.passwordFreebox.value;
		
		options.distantAccess = $this.distantAccess.checked;
		options.distantAccessIP = $this.distantAccessIP.value;
		options.distantAccessPort = $this.distantAccessPort.value;
		
		options.torrentAdvanced = $this.torrentAdvanced.checked;
		options.torrentContextMenu = $this.torrentContextMenu.checked;
		
		var torrentExceptions = [];
		
		for (var i = 0; i < $this.selectTorrentExceptions.options.length; i++) {
			var opt = $this.selectTorrentExceptions.options[i];
			torrentExceptions.push(opt.value);
		}
		
		options.torrentExceptions = torrentExceptions;
		
		options.saveToStorage();
		
		$this.checkDistantAccessPermission();
	};
	
	this.shortcutDistantAccessIP = function(e) {
		$this.autocompleteIPPort($this.distantAccessIP.value);
	};
	
	this.checkDistantAccessPermission = function() {
		if ($this.distantAccess.checked && !FreeboxLinkAPI.hasPermissionOrigin("http://*/*")) {
			FreeboxLinkAPI.askPermissionOrigin("http://*/*", $this.checkDistantAccessPermission.callback);
		} else if (!$this.distantAccess.checked && FreeboxLinkAPI.hasPermissionOrigin("http://*/*") && !$this.torrentAdvanced.checked) {
			FreeboxLinkAPI.removePermissionOrigin("http://*/*");
		}
	};
	
	this.checkDistantAccessPermission.callback = function(b) {
		console.log(b);
	};
	
	this.documentready = function() {
		for (var i = 0; i < informationIcons.length; i++) {
			var title = TEXTS["aide-"+informationIcons[i]];
			JSD.$(informationIcons[i]).setAttribute("title",title);
		}
		
		$this.selectTorrentExceptions = new SelectDeletable("#torrent-exceptions");
		$this.loadFromOptions();
		
		JSD.on("click",$this.btnSave, $this.saveToOptions);
		JSD.on("click",$this.torrentExceptionsAdd, $this.addURLToTorrentExceptions);
		JSD.on("change", $this.distantAccess, $this.distantAccessCheckChange);
		JSD.on("keyup", $this.distantAccessIP, $this.shortcutDistantAccessIP);
		$this.distantAccessCheckChange();
		$this.checkDistantAccessPermission();
	};
	
})();

Page.load(PageOptionsGenerales);